import json

from py2neo import Graph, Node, Relationship, Subgraph, NodeMatcher


class Neo4j:
    def __init__(self, path, batch_size=10):
        self.graph = Graph("bolt://8.146.204.90:7687", auth=("neo4j", "0123456789"))
        self.path = path
        self.batch_size = batch_size
        self.nodes = []

    # 一行变成一个node
    def get_node(self, line: dict) -> Node:
        # if line["dish"] == 'Unknown':
        #     node = Node("dish", name=line["name"],description=line["description"])
        # else:
        #     node = Node("dish", name=line["dish"],description=line["description"],recipeIngredient=line["recipeIngredient"])
        node = Node("dish", name=line["name"], dish=line['dish'], description=line["description"],
                    recipeIngredient=line["recipeIngredient"],
                    author=line["author"],
                    recipeInstructions=line["recipeInstructions"],
                    keywords=line["keywords"])
        return node

    def to_neo4j2(self):
        count = 0
        with open(self.path, 'r', encoding='utf-8') as f:
            while True:
                line = f.readline()  # 调用文件的 readline()方法
                if line == '':
                    break
                json_data = json.loads(line)
                node = self.get_node(json_data)
                self.nodes.append(node)
                count += 1
                if count == self.batch_size:
                    count = 0
                    self.graph.create(Subgraph(self.nodes))

                    self.nodes.clear()

            if len(self.nodes) != 0:
                self.graph.create(Subgraph(self.nodes))
                self.nodes.clear()

    def find(self,name:str):
        nodes = NodeMatcher(self.graph)
        node_single = nodes.match("dish", name=name).first()
        return node_single



#     def to_neo4j(self):
#         with open(self.path, 'r', encoding='utf-8') as f:
#             line = f.readline()  # 调用文件的 readline()方法
#             json_data = json.loads(line)
#             node = self.get_node(json_data)
#             self.nodes.append(node)
#             i = 1
#             while line:
#                 i = i + 1
#                 line = f.readline()
#                 if line == '':
#                     break
#
#                 node = self.get_node(json_data)
#                 self.nodes.append(node)
#
#                 print(json_data)
#                 if i > 5:
#                     break
#
#
# graph = Graph("bolt://8.146.204.90:7687", auth=("neo4j", "0123456789"))
# keyset = set()
#
#
# # Read json file
#
# def write_line_to_neo4j(line: dict) -> Node:
#     # if line["dish"] == 'Unknown':
#     #     node = Node("dish", name=line["name"],description=line["description"])
#     # else:
#     #     node = Node("dish", name=line["dish"],description=line["description"],recipeIngredient=line["recipeIngredient"])
#     node = Node("dish", name=line["name"], dish=line['dish'], description=line["description"],
#                 recipeIngredient=line["recipeIngredient"],
#                 author=line["author"],
#                 recipeInstructions=line["recipeInstructions"],
#                 keywords=line["keywords"])
#     return node
#     # graph.create(node)
#
#     # node = Node("recipeIngredient", name=line["dish"])
#
#
# print(keyset)
# print("end")
if __name__ == '__main__':
    neo = Neo4j(path="data/demo.json",batch_size=2)
    # neo.to_neo4j2()
    print(neo.find("快手烤鸡翅"))
